package com.hatic.process;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.StringUtil;
import org.jxstar.util.factory.FactoryUtil;

/**
 * 取项目相关附件ID。
 *
 * @author TonyTan
 * @version 1.0, 2011-12-14
 */
public class ProjectAttachBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	private static Map<String,String> _mpTableSql = FactoryUtil.newMap();
	static{
		_mpTableSql = getTableSql();
	}
	
	/**
	 * 根据投资计划ID取相关附件
	 * @param planId
	 * @return
	 */
	public String queryAttach4Plan(String planId) {
		String projectId = getProjectId(planId);
		String ins = queryAttachIds(projectId);
		
		//根据计划ID取附件
		String pids = getPlanAttachId(planId);
		if (pids.length() > 0) {
			if (ins.length() > 0) {
				ins += "," + pids;
			} else {
				ins = pids;
			}
		}
		
		_log.showDebug("..............all attach ids: " + ins);
		setReturnData("{ids:'" + StringUtil.strForJson(ins) + "'}");
		
		return _returnSuccess;
	}

	/**
	 * 根据项目ID取相关附件
	 * @param projectId
	 * @return
	 */
	public String queryAttach(String projectId) {
		String ins = queryAttachIds(projectId);
		
		_log.showDebug("..............all attach ids: " + ins);
		setReturnData("{ids:'" + StringUtil.strForJson(ins) + "'}");
		
		return _returnSuccess;
	}
	
	private String queryAttachIds(String projectId) {
		if (_mpTableSql.isEmpty()) {
			setMessage("没有找到附件表定义文件【ProjectAttach.properties】");
			return _returnFaild;
		}
		
		StringBuilder allIds = new StringBuilder();
		Iterator<String> itr = _mpTableSql.keySet().iterator();
		while(itr.hasNext()) {
			String key = itr.next();
			String sql = _mpTableSql.get(key);
			String ids = getAttachId(projectId, key, sql);
			//_log.showDebug("..............has attach table: " + key + "; ids: " + ids);
			
			if (ids.length() > 3) {
				allIds.append(ids+",");
			}
		}
		
		String ins = "";
		if (allIds.length() > 3) {
			ins = allIds.substring(0, allIds.length()-1);
		}
		return ins;
	}
	
	//找一个业务表的附件信息
	private String getAttachId(String projectId, String tableName, String mysql) {
		String sql = "select attach_id from sys_attach where table_name = ? and data_id in " +
					 "("+ mysql +")";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(tableName);
		param.addStringValue(projectId);
		
		return getAttachId(_dao.query(param));
	}
	
	//根据计划ID取项目ID
	private String getProjectId(String planId) {
		String sql = "select project_id from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		if (mpData.isEmpty()) return "";
		
		return mpData.get("project_id");
	}
	
	//投资计划取附件时用，可能存在投资计划还没创建项目记录
	private String getPlanAttachId(String planId) {
		String sql = "select attach_id from sys_attach where table_name = 'money_plan' and data_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return getAttachId(_dao.query(param));
	}
	
	private String getAttachId(List<Map<String, String>> lsData) {
		if (lsData == null || lsData.isEmpty()) return "";
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0, n = lsData.size(); i < n; i++) {
			String id = lsData.get(i).get("attach_id");
			sb.append("'"+id+"',");
		}
		
		String ids = "";
		if (sb.length() > 3) {
			ids = sb.substring(0, sb.length()-1);
		}
		return ids;
	}
	
	/**
	 * 取定义的业务表信息
	 * @return
	 */
	private static Map<String,String> getTableSql() {
		try {
			String BUNDLE_NAME = "com.hatic.process.ProjectAttach";
			ResourceBundle resource = ResourceBundle.getBundle(BUNDLE_NAME);
			
			Enumeration<String> en = resource.getKeys();
			while(en.hasMoreElements()) {
				String table = en.nextElement();
				
				String value = resource.getString(table);
				_mpTableSql.put(table, "select " + value + " as keyid from " + table + " where project_id = ?");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return _mpTableSql;
	}
}
